home *** CD-ROM | disk | FTP | other *** search
/ Delphi Magazine Collection 2001 / Delphi Magazine Collection 20001 (2001).iso / DISKS / Issue50 / IPC / Named Pipes / API / ChildMainFormUnit.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1999-08-28  |  1.7 KB  |  79 lines

  1. unit ChildMainFormUnit;
  2.  
  3. interface
  4.  
  5. uses
  6.   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  7.   StdCtrls;
  8.  
  9. type
  10.   TForm1 = class(TForm)
  11.     Edit1: TEdit;
  12.     Button1: TButton;
  13.     procedure FormCreate(Sender: TObject);
  14.     procedure FormDestroy(Sender: TObject);
  15.     procedure Button1Click(Sender: TObject);
  16.   private
  17.     PipeWrite: THandle;
  18.   end;
  19.  
  20. {$ifdef Ver90}
  21.   //This exception class did not exist in Delphi 2
  22.   EWin32Error = class(Exception);
  23. {$endif}
  24.  
  25. var
  26.   Form1: TForm1;
  27.  
  28. const
  29.   PipeNameFixedPrefix = '\\.\pipe\';
  30.   PipeName = PipeNameFixedPrefix + 'SampleNamedPipe';
  31.  
  32. implementation
  33.  
  34. {$R *.DFM}
  35.  
  36. {$ifdef Ver90}
  37. //This function class did not exist in Delphi 2
  38. function Win32Check(RetVal: Bool): Bool;
  39. var
  40.   LastError: DWord;
  41. begin
  42.   Result := RetVal;
  43.   if not RetVal then
  44.   begin
  45.     LastError := GetLastError;
  46.     if LastError <> Error_Success then
  47.       raise EWin32Error.CreateFmt( 'Win32 Error.  Code: %d.'#10'%s',
  48.         [LastError, SysErrorMessage(LastError)])
  49.     else
  50.       raise EWin32Error.Create('A Win32 API function failed')
  51.   end;
  52. end;
  53. {$endif}
  54.  
  55. procedure TForm1.FormCreate(Sender: TObject);
  56. begin
  57.   PipeWrite := CreateFile(PipeName, Generic_Write, 0,
  58.     nil, Open_Existing, File_Attribute_Normal, 0);
  59.   if PipeWrite = Invalid_Handle_Value then
  60.     raise EWin32Error.Create('Cannot open pipe for writing');
  61. end;
  62.  
  63. procedure TForm1.FormDestroy(Sender: TObject);
  64. begin
  65.   CloseHandle(PipeWrite);
  66. end;
  67.  
  68. procedure TForm1.Button1Click(Sender: TObject);
  69. var
  70.   BytesWritten: DWord;
  71.   Msg: String;
  72. begin
  73.   Msg := Edit1.Text + #13#10;
  74.   Win32Check(WriteFile(PipeWrite, Msg[1], Length(Msg),
  75.     BytesWritten, nil));
  76. end;
  77.  
  78. end.
  79.